package com.example.spzx.Service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.spzx.Service.ValidateCodeService;
import com.example.spzx.model.vo.system.ValidateCodeVo;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public ValidateCodeVo generateValidateCode(){
        CircleCaptcha circleCaptcha= CaptchaUtil.createCircleCaptcha(150,48,4,20);
        String codeValue=circleCaptcha.getCode();
        String imageBase64=circleCaptcha.getImageBase64Data();
        String codeKey= UUID.randomUUID().toString().replace("-", "");
        stringRedisTemplate.opsForValue().set("user:login:validatecode:"+codeKey,codeValue,5, TimeUnit.MINUTES);
        ValidateCodeVo validateCodeVo=new ValidateCodeVo();
        validateCodeVo.setCodeKey(codeKey);
        validateCodeVo.setCodeValue(imageBase64);

        return validateCodeVo;
    }
}
